﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using AvalonDock;
using RiskMan.Config;
using RiskMan.DataClass.Models;
using RiskMan.DataClass.Models.NotBasModels;
using RiskMan.DataClass.Stops;
using RiskMan.Views.Dialogs;

namespace RiskMan.Views.Stop
{
    /// <summary>
    /// Interaction logic for StopView.xaml
    /// </summary>
    public partial class StopView : DocumentContent
    {
        private MainWindow mainWindow;
        private DispatcherTimer _dsUpdate;
        private string _formate;


        public StopView()
        {
            mainWindow = MainWindow.StaticLevel;
            InitializeComponent();
            this.Title = Cfg.Stops;
            DtgdStops.ItemsSource = mainWindow._dataProvider.StopList;
        }

        private void BtnDelete_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            bool flagChek = false;
            try
            {
                for (int i = 0; i < mainWindow._dataProvider.StopList.Count; i++)
                {
                    if (mainWindow._dataProvider.StopList[i].Check == true)
                    {
                        mainWindow._dataProvider.StopList.Remove(mainWindow._dataProvider.StopList[i]);

                        flagChek = true;
                        i--;
                    }
                }
            }
            catch
            {

            }

            if (flagChek == false)
                new DialogMessage("Не выбрано ни одного элемента для удаления", "Внимание!");
        }

        private void BtnCopy_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DtgdStops.SelectedItems.Count == 1)
            {
                int index = DtgdStops.SelectedIndex + 1;
                //dtgdLevel.UnselectAll();
                try
                {
                    if (new DialogOkCancel("Вы действительно хотите скопировать строку " + index + "?", "Внимание").Result == MessageBoxResult.OK)
                    {
                        foreach (var item in mainWindow._dataProvider.StopList)
                        {
                            if (item.GuidRobot == ((StopsModel)(DtgdStops.SelectedItem)).GuidRobot)
                            {
                                var tmp = (StopsModel)Cfg.DeepClone(item);
                                tmp.IsStart = false;
                                tmp.GuidRobot = Guid.NewGuid();
                                tmp.TradeList = new System.Collections.ObjectModel.ObservableCollection<TradesOpen>();
                                mainWindow._dataProvider.StopList.Add(tmp);
                            }
                        }

                    }
                }
                catch
                { }
            }
        }

        private void BtnAdd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            StopsModel slm = new StopsModel();
            Test newStop = new Test(slm);
            newStop.ShowDialog();

            if (Convert.ToBoolean(newStop.DialogResult))
                mainWindow._dataProvider.StopList.Add(slm);
        }

        private void BtnStopCheck_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            foreach (var item in mainWindow._dataProvider.StopList)
            {
                if (item.Check)
                    item.IsStart = false;
            }
        }

        private void BtnCheckStart_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            foreach (var item in mainWindow._dataProvider.StopList)
            {
                if (item.Check)
                    item.IsStart = true;
            }
        }


        private void DtgdStops_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            StopsModel slm = (StopsModel)(((DataGrid)sender).SelectedItem);
            Test newStop = new Test(slm);
            newStop.ShowDialog();
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < mainWindow._dataProvider.StopList.Count; i++)
            {
                mainWindow._dataProvider.StopList[i].Check = (sender as CheckBox).IsChecked.Value;
            }
            DtgdStops.ItemsSource = mainWindow._dataProvider.StopList;
        }
    }
}
